package yxw.com.smartbutler.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import org.w3c.dom.Text;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;
import yxw.com.smartbutler.MainActivity;
import yxw.com.smartbutler.R;
import yxw.com.smartbutler.entity.MyUser;
import yxw.com.smartbutler.utils.L;
import yxw.com.smartbutler.utils.ShareUtils;

/*
 *  项目名： SmartButler
 *  包名:    yxw.com.smartbutler.ui
 *  文件名： LoginActivity
 *  创建者：小威.岳
 *  创建时间：2018/11/14 15:17
 *  描述： 登录
 */public class LoginActivity extends AppCompatActivity implements View.OnClickListener{

     //注册按钮
    private Button btn_regist;

    private EditText et_name;
    private EditText et_pwd;
    private Button btn_login;
    //记住密码
    private CheckBox keep_pwd;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        initView();
    }

    private void initView() {
        btn_regist=findViewById(R.id.btn_regist);
        btn_regist.setOnClickListener(this);
        et_name=findViewById(R.id.et_name);
        et_pwd=findViewById(R.id.et_pwd);
        btn_login=findViewById(R.id.btn_login);
        btn_login.setOnClickListener(this);
        keep_pwd=findViewById(R.id.keep_pwd);
        //设置选中的状态
        boolean isCheck=ShareUtils.getBoolean(this,"keeppass",false);
        keep_pwd.setChecked(isCheck);
        if(isCheck){
            //设置密码
            et_name.setText(ShareUtils.getString(this,"name",""));
            et_pwd.setText(ShareUtils.getString(this,"pwd",""));
        }

    }

    @Override
    public void onClick(View view) {
        switch(view.getId()){
            case R.id.btn_regist:
                startActivity(new Intent(this,RegistActivity.class));
                break;
            case R.id.btn_login:
               String name= et_name.getText().toString().trim();
                String pwd= et_pwd.getText().toString().trim();

                if(!TextUtils.isEmpty(name) & !TextUtils.isEmpty(pwd)){
                   final MyUser user= new MyUser();
                    user.setUsername(name);
                    user.setPassword(pwd);
                    user.login(new SaveListener<MyUser>() {


                        @Override
                        public void done(MyUser myUser, BmobException e) {
                            if(e==null){
                                //判断邮箱是否验证
                                if(user.getEmailVerified()){
                                    //跳转
                                   startActivity(new Intent(LoginActivity.this,MainActivity.class));
                                    finish();
                                }else{
                                    Toast.makeText(LoginActivity.this, "请前往邮箱验证", Toast.LENGTH_SHORT).show();
                                }

                            }else{
                                L.i(e.toString());
                                Toast.makeText(LoginActivity.this, "登录失败："+e.getErrorCode(), Toast.LENGTH_SHORT).show();
                            }
                        }
                    });

                }else{
                    Toast.makeText(LoginActivity.this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    /**
     * 假设我现在输入用户名和密码，但是我不点击登录，而是直接退出了
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        //保存状态
        ShareUtils.putBoolean(this,"keeppwd",keep_pwd.isChecked());
//是否记住密码
        if(keep_pwd.isChecked()){
            ShareUtils.putString(this,"name",et_name.getText().toString().trim());
            ShareUtils.putString(this,"pwd",et_pwd.getText().toString().trim());
        }else{
            ShareUtils.delShare(this,"name");
            ShareUtils.delShare(this,"pwd");
        }
    }


}
